// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    defaultDate: '',
    daysList: [],
    daysData: [],
    content: [],
    date:'',
    isDataChange: false,
    today: false,
  },
  onLoad() {
    let daysList = [
      {
        date: '10',
        show: true
      },
      {
        date: '13',
        show: true
      },
      {
        date: '24',
        show: true
      }
    ]
    this.setData({
      defaultDate: '2022/4/10',
      date: '2022/4/10',
      // daysList: daysList
    })
   
  },
  onshow() {

  },
  select(res) {
    // wx.showToast({
    //   title: res.detail,
    //   icon: 'success'
    // })
    console.log(res.detail)
    this.setData({
      content: res.detail.content,
      date:res.detail.Y+'/'+res.detail.M+'/'+res.detail.D
    })
  },
  changeDate() {
    var date = new Date();
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    this.setData({
      defaultDate: year + '/' + month + '/' + day,
      today:!this.data.today
    })
  },
  //点击删除日程
  deleteCilck(e) {
    wx.showModal({
      title: '提示',
      content: '确认删除该日程吗',
      success: (res) => {
        if (res.confirm) {
          var datea = this.data.date;
          var dateb = datea.split('/');
          var swiper_data = wx.getStorageSync('swiper_data');
          swiper_data[dateb[1] - 1].forEach(ele => {
            if (ele.date == dateb[2]) {
              var index = ele.content.findIndex((profile) => profile.title === e.target.dataset.con);
              console.log(e.target.dataset.con,'1')
              console.log(ele.content,'2')
              if (index !== -1) {
                ele.content.splice(index, 1);
              }
              if(ele.content.length==0){
                ele.topText = '';
              }
              
            }
          })
          wx.setStorageSync('swiper_data', swiper_data);
          this.setData({
            isDataChange: !this.data.isDataChange // 用户数据改变了
          })
        }
      }
    })
  },
  changeData() {
    let dt = [
      {
        date: '11',
        show: true
      },
      {
        date: '12',
        show: true
      },
      {
        date: '19',
        show: true
      },
      {
        date: '22',
        show: true
      }
    ]
    this.setData({
      daysList: dt,
      isDataChange: !this.data.isDataChange // 用户数据改变了
    })
  },
  formatter(res) {
    // res.detail 为日历组件传出的日期数据数组
    console.log(res.detail)
    let da = res.detail;
    let dl = this.data.daysList;

    // 先清空
    da.forEach(x => { x.topText = ''; x.bottomText = ''; });

    // 再赋值
    for (let i = 0, len = dl.length; i < len; i++) {
      if (dl[i].show) {
        let model = da.find(x => x.date == dl[i].date);
        model && (model.bottomText = '预约(4)');
      }
    }

    // 将处理后的日期数据再传入日历组件
    this.setData({ daysData: da });
  }
})
